<?php
/**
 * Description of SearchController
 *
 * @author Алексей
 */
class SearchController extends CController
{
	public $layout = '//layouts/main';
	
	/**
	 * Форма поиска
	 */
	public function actionIndex()
	{	
		$manufacturers = Catalog::getManufacturers();
		$this->render('index', array(
			'manufacturers' => $manufacturers
		));
	}
	
	/**
	 * Поиск деталей
	 */
	public function actionProcess()
	{
		// Поиск по типу ТС
		if(isset($_POST['type']) && intval($_POST['type'])){
			$searchId = Catalog::getSparesByTypeID(intval($_POST['type']));
			$this->redirect($this->createUrl('/catalog/search/results', array('id'=>$searchId)));	
		}
		// По артикулу
		elseif(isset($_POST['code']) && !empty($_POST['code'])){
			$searchId = Catalog::getSparesByCode($_POST['code']);
			$this->redirect($this->createUrl('/catalog/search/results', array('id'=>$searchId)));	
		}
	}
	
	/**
	 * Список результатов поиска
	 */
	public function actionResults($id, $page=1)
	{	
		$pageSize = 50;
		$page--;
		$results = Catalog::getSearchResults($id, $pageSize, $page);
		$pages = new CPagination($results['count']);
		$pages->pageSize = $pageSize;
		
		Context::getInstance()->addWidget('application.components.widgets.ArticlesCategories', array(
			'articles'=>$results['articles'],
		));
		
		$this->render('results', array(
			'results'=>$results,
			'pages'=>$pages
		));
	}
}

?>
